package com.junjiao.util.java.servlet;

import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.math.BigDecimal;
import java.net.URL;
import java.net.URLConnection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Random;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;

import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import com.junjiao.util.java.XmlBuildUtil;
/**
 * Servlet工具类
 *
 * @author jiaojun
 * @email:junjiao.j@gmail.com
 * @version:v0.2
 * @date:2013-1-29
 */
public class ServletUtil {

	public static final String ENCODING = "UTF-8";

	/**
	 * 输出XML信息
	 * 
	 * @param response
	 * @param xmlStr
	 * @throws IOException
	 */
	public static void outputXML(HttpServletResponse response, String xmlStr)
			throws IOException {
		response.setContentType("text/html;charset=" + ENCODING);
		PrintWriter out = response.getWriter();
		out.println(xmlStr);
		out.close();
	}

	/**
	 * 输出XML信息
	 * 
	 * @param response
	 * @param xmlDoc
	 * @throws IOException
	 */
	public static void outputXML(HttpServletResponse response, Document xmlDoc)
			throws IOException {
		ServletOutputStream out = response.getOutputStream();
		XmlBuildUtil.writeXMlResponse(out, xmlDoc, ENCODING);
	}

	public static Document buildNullDoc() {
		Document doc = DocumentHelper.createDocument();
		doc.addElement("objectSet");
		return doc;
	}

	/**
	 * "标题标题"--转化后---"标题..."
	 * 
	 * @param str
	 * @param length
	 * @return
	 */
	public static String regularText(String str, int length) {
		if (length == 0) {
			return str;
		}
		String temp = str;

		if (temp == null || temp.equals("")) {
			return "暂无";
		}
		if (temp.length() > length + 1) {
			temp = temp.substring(0, length) + "...";
		}
		return temp;
	}

	/**
	 * "标题标题"--转化后---"标题..."
	 * 
	 * @param str
	 * @param length
	 * @return
	 */
	public static String regularText3(String str, int length) {
		if (length == 0) {
			return str;
		}
		String temp = str;

		if (temp == null || temp.equals("")) {
			return "";
		}
		if (temp.length() > length + 1) {
			temp = temp.substring(0, length) + "...";
		}
		return temp;
	}

	/**
	 * 
	 * @param str
	 * @param length
	 * @return
	 */
	public static String regularText2(String str, int length) {
		if (length == 0) {
			return str;
		}
		String temp = str;

		if (temp == null || temp.equals("")) {
			return "暂无";
		}
		if (temp.length() > length + 1) {
			temp = temp.substring(0, length) + "";
		}
		return temp;
	}

	public static String regularText4(String str, int length) {
		if (length == 0) {
			return str;
		}
		String temp = str;

		if (temp == null || temp.equals("")) {
			return "暂无";
		}
		if (temp.length() > length) {
			temp = temp.substring(0, length) + "";
		}
		return temp;
	}

	/**
	 * 便民热线电话
	 * 
	 * @param big
	 * @param length
	 * @return
	 */
	public static String regularBig(BigDecimal big, int length) {
		if (length == 0) {
			return big.toString();
		}
		String temp = null;
		if (big != null) {
			temp = big.toString();
		}
		if (temp == null || temp.equals("")) {
			return "暂无";
		}
		if (temp.length() > length + 1) {
			temp = temp.substring(0, length) + "...";
		}
		return temp;
	}

	public static String regularBig2(BigDecimal big, int length) {
		if (length == 0) {
			return big.toString();
		}
		String temp = null;
		if (big != null) {
			temp = big.toString();
		}

		if (temp == null || temp.equals("")) {
			return "暂无";
		}
		if (temp.length() > length + 1) {
			temp = temp.substring(0, length) + "";
		}
		return temp;
	}

	/**
	 * 通过url得到Xml
	 * 
	 * @param url
	 * @return
	 */
	public static Document getXmlByUrl(String url) {
		Document doc = null;
		InputStream in = null;
		try {
			URL urls = new URL(url.toString());
			URLConnection urlConn = urls.openConnection();
			in = urlConn.getInputStream();
			SAXReader saxReader = new SAXReader();
			doc = saxReader.read(in);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (in != null) {
				try {
					in.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		return doc;
	}

	/**
	 * 数字随机码
	 * 
	 * @param length
	 * @return
	 */
	public static String getRandomCode(int length) {

		Random r = new Random();

		int temp1, temp2;
		int send[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
		int len = send.length;
		String returnValue = "";
		for (int i = 0; i < length; i++) {
			temp1 = Math.abs(r.nextInt()) % len;
			returnValue += "" + send[temp1];
			temp2 = send[temp1];
			send[temp1] = send[len - 1];
			send[len - 1] = temp2;
			len--;
		}
		return returnValue;

	}

	public static String[] copyToArray(Object[] o) {
		String[] arr = new String[o.length];
		for (int i = 0; i < o.length; i++) {
			arr[i] = o[i].toString();
		}
		return arr;
	}

	// 取得doc子节点的值
	public static String getElementText(Document doc, String element) {
		Element root = doc.getRootElement();
		Element ele = root.element(element);
		String str = ele.getText();
		if (str == null) {
			str = "";
		}
		return str;
	}

	// 取得所有子节点及值
	public static Map getElementAndText(Document doc) {
		Map map = new HashMap();
		Element root = doc.getRootElement();
		for (Iterator i = root.elementIterator(); i.hasNext();) {
			Element element = (Element) i.next();
			map.put(element.getName(), element.getText());
		}
		return map;
	}

	// 字符串转XML
	public static Document strToDoc(String str, String encode) throws Exception {
		String term = null;
		Document doc = null;
		try {
			if (encode != null && !"".equals(encode)) {
				term = new String(str.getBytes("iso-8859-1"), encode);
			} else {
				term = str;
			}
			doc = DocumentHelper.parseText(term);
		} catch (Exception e) {
			doc = null;
			throw new Exception("参数错误,或者乱码!  传入参数为:" + term);
		}
		return doc;
	}

}
